{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from github import Github\n",
    "import openpyxl\n",
    "\n",
    "# 初始化 GitHub API 客户端\n",
    "g = Github(\"your_github_token\")\n",
    "\n",
    "# 创建用户表格\n",
    "wb_user = openpyxl.Workbook()    # 创建用户工作簿\n",
    "ws_user = wb_user.active        # 获取当前活动表格\n",
    "ws_user.title = \"用户数据\"    # 设置表格标题\n",
    "ws_user.append([\"用户ID\", \"项目名称\", \"用户提交数\"])    # 定义表格头\n",
    "\n",
    "# 创建项目表格\n",
    "wb_project = openpyxl.Workbook()    # 创建项目工作簿\n",
    "ws_project = wb_project.active\n",
    "ws_project.title = \"项目详细数据\"\n",
    "ws_project.append([\n",
    "    \"项目名称\", \"stars数\", \"forks数\", \"issues数\", \"watchers数\", \n",
    "    \"项目总提交数\", \"项目最新更新时间\"\n",
    "])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用户ID列表\n",
    "user_ids = [\"mojombo\", \"defunkt\", \"pjhyett\"]  # 可扩展到更多用户"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取用户参与的项目、提交次数、关键提交次数\n",
    "\n",
    "def get_user_projects(user_id):\n",
    "    user = g.get_user(user_id)    # 获取用户对象\n",
    "    repos = user.get_repos()      # 获取用户的项目列表\n",
    "    for repo in repos:            # 遍历用户的项目列表\n",
    "        try:\n",
    "            # 获取用户在该项目的提交次数\n",
    "            stats = repo.get_stats_contributor(user)\n",
    "            user_commits = stats.total if stats else 0\n",
    "\n",
    "            # 将用户的项目数据写入用户表格\n",
    "            ws_user.append([user_id, repo.name, user_commits])\n",
    "        except Exception as e:\n",
    "            print(f\"获取用户 {user_id} 在项目 {repo.name} 上的数据时出错: {e}\")\n",
    "\n",
    "# 批量获取用户的项目数据\n",
    "for user_id in user_ids:\n",
    "    get_user_projects(user_id)\n",
    "\n",
    "# 保存用户项目数据到Excel文件\n",
    "wb_user.save(\"用户数据.xlsx\")\n",
    "print(\"用户项目数据已保存到 '用户数据.xlsx'\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取项目相关详细信息\n",
    "\n",
    "# 提取项目的唯一列表\n",
    "unique_projects = set()\n",
    "\n",
    "# 读取用户数据\n",
    "for row in ws_user.iter_rows(min_row=2, values_only=True):\n",
    "    unique_projects.add(row[1])\n",
    "\n",
    "def get_project_details(repo_name):\n",
    "    try:\n",
    "        repo = g.get_repo(repo_name)  # 获取项目仓库信息\n",
    "        # 获取项目的详细信息\n",
    "        stars = repo.stargazers_count  # 获得项目的星标数\n",
    "        forks = repo.forks_count  # 获得项目的分叉数\n",
    "        open_issues = repo.open_issues_count  # 获得项目的开放问题数\n",
    "        watchers = repo.watchers_count  # 获得项目的关注者数\n",
    "        total_commits = repo.get_commits().totalCount  # 获得项目的总提交数\n",
    "        last_updated = repo.updated_at  # 获得项目的最后更新时间\n",
    "\n",
    "        # 将项目数据写入项目表格\n",
    "        ws_project.append([\n",
    "            repo_name, stars, forks, open_issues, watchers,\n",
    "            total_commits,  last_updated\n",
    "        ])\n",
    "    except Exception as e:\n",
    "        print(f\"获取项目 {repo_name} 数据时出错: {e}\")\n",
    "\n",
    "# 批量获取项目信息\n",
    "for repo_name in unique_projects:\n",
    "    get_project_details(repo_name)\n",
    "\n",
    "# 保存项目数据到Excel文件\n",
    "wb_project.save(\"项目详细数据.xlsx\")\n",
    "print(\"项目详细数据已保存到 '项目详细数据.xlsx'\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "TIH",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
